package com.yk.i_wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yk.i_wms.common.model.CheckWarehouseEnum;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.mapper.SmsReceiveDocDetailMapper;
import com.yk.i_wms.model.SmsReceiveDocDetail;
import com.yk.i_wms.model.SmsStockInfo;
import com.yk.i_wms.service.ISmsReceiveDocDetailService;
import com.yk.i_wms.service.ISmsStockInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 入库单详情
 *
 * @author hbq
 * @date 2021-04-01 17:36:42
 */
@Slf4j
@Service
public class SmsReceiveDocDetailServiceImpl extends ServiceImpl<SmsReceiveDocDetailMapper, SmsReceiveDocDetail> implements ISmsReceiveDocDetailService {
    @Resource
    private SmsReceiveDocDetailMapper smsReceiveDocDetailMapper;
    @Autowired
    private ISmsStockInfoService smsStockInfoService;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<Map> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<Map> pages = new Page<>(pageNum, pageSize);
        return smsReceiveDocDetailMapper.findList(pages, params);
    }

    @Override
    public Result check(SmsReceiveDocDetail smsReceiveDocDetail) {
        //更新入库单详情
        updateById(smsReceiveDocDetail);
        SmsReceiveDocDetail receiveDocDetail = getById(smsReceiveDocDetail);

        //更新库存表
        List<SmsStockInfo> stockInfos = smsStockInfoService.list(new LambdaQueryWrapper<SmsStockInfo>()
                .eq(SmsStockInfo::getDocNo, receiveDocDetail.getDocNo())
                .eq(SmsStockInfo::getPoNo, receiveDocDetail.getPoNo())
                .eq(SmsStockInfo::getItemCode, receiveDocDetail.getItemCode())
                .eq(SmsStockInfo::getBatch, receiveDocDetail.getBatch())
        );
        stockInfos.forEach(
                u->{
                    u.setCheckState(receiveDocDetail.getCheckState());
                    if (receiveDocDetail.getCheckState()==CheckWarehouseEnum.S_4.getCheckState()) {
                        u.setWhCode("M-UPT");
                    }
                    u.setWarehouse(
                            receiveDocDetail.getCheckState()==CheckWarehouseEnum.S_1.getCheckState() ||
                                    receiveDocDetail.getCheckState()==CheckWarehouseEnum.S_3.getCheckState() ?
                                    CheckWarehouseEnum.S_1.getWarehouse():CheckWarehouseEnum.S_4.getWarehouse());
                }
        );
        smsStockInfoService.updateBatchById(stockInfos);
        return Result.succeed("保存成功");
    }
}
